layui.use(['layer','upload','form'],function(){
    var layer = parent.layer === undefined ? layui.layer : top.layer
        ,$ = layui.jquery
        ,upload = layui.upload
        ,form = layui.form;
    var ids = $("#ids").val();
    var type = $("#type").val();
    var dataLoad;
    var uploadInst = upload.render({
        elem: '#photoFile', //绑定元素
        accept: 'images', //允许上传的文件类型
        acceptMime: 'image/jpg, image/jpeg, image/png', //'image/*'
        multiple: false, //允许多文件上传
        auto: false, //选完文件后不要自动上传
        bindAction: '#sub-photobtn', //指定一个按钮触发上传
        url: '/reupOwnStuPhoto', //上传接口
        data: {
            ids: function(){
                return $('#ids').val();
            }
            ,type: function(){
                return $('#type').val();
            }
        },
        choose: function(obj){
            var files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //图像预览，如果是多文件，会逐个添加。(不支持ie8/9)
            obj.preview(function(index, file, result){
                var imgobj = new Image(); //创建新img对象
                imgobj.src = result; //指定数据源
                imgobj.className = 'thumb';
                imgobj.onclick = function(result) {
                    //单击预览
                    var w = $(window).width() - 42, h = $(window).height() - 42;
                    layer.open({
                        type: 1,
                        skin: 'none', //加上边框
                        area: ['45%', '80%'], //宽高
                        //area: [w, h], //宽高
                        shadeClose: true, //开启遮罩关闭
                        end: function (index, layero) {
                            return false;
                        },
                        content: '<div style="text-align:center"><img src="' + this.src + '" /></div>'
                    });
                };
                document.getElementById('div_prev').innerHTML = "";
                document.getElementById("div_prev").appendChild(imgobj);
            });
        },
        before: function(){
            //dataLoad = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            dataLoad = layer.load(3,{
                shade:0.3
            });
        },
        done: function(res){
            layer.close(dataLoad);
            if (res.msg =="success") {
                top.layer.msg("添加成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }else if (res.msg == "error") {
                top.layer.msg("添加失败！");
            }else{
                top.layer.msg(res.msg);
            }
        },
        error: function(){
            layer.msg("文件上传失败.");
        }
    });
    form.on("submit(photoImp)",function(data){
        if (ids == undefined || ids==null || ids=='') {
            top.layer.msg("参数异常，请重新尝试。");
            return false;
        }
        return false;
    });
    $(".cancel").on('click', function () {
        layer.closeAll("iframe");
        parent.location.reload();
    });
})
